﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to Program.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (Program.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DishDash.Common;
using MessageAPI;

#endregion

namespace PerformanceEventGenerator
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var eventTypes = new List<string>();

            eventTypes.Add("PerformanceEventGenerator.EventTypeA.callcount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeB.callcount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeC.callcount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeD.callcount");

            eventTypes.Add("PerformanceEventGenerator.EventTypeA.exceptionCount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeB.exceptionCount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeC.exceptionCount");
            eventTypes.Add("PerformanceEventGenerator.EventTypeD.exceptionCount");

            eventTypes.Add("PerformanceEventGenerator.EventTypeA.duration");
            eventTypes.Add("PerformanceEventGenerator.EventTypeB.duration");
            eventTypes.Add("PerformanceEventGenerator.EventTypeC.duration");
            eventTypes.Add("PerformanceEventGenerator.EventTypeD.duration");

            Parallel.ForEach(eventTypes, eventType =>
                                             {
                                                 MessageAPI.IPerformanceInstrumentationServiceFactory factory =
                                                     new MessageAPI.Core.Implementation.
                                                         PerformanceInstrumentationServiceFactory();
                                                 IPerformanceInstrumentationService service =
                                                     factory.GetPerformanceInstrumentationService();

                                                 var randomNumberOfEventsToGenerate = new Random();
                                                 var randomNumber = randomNumberOfEventsToGenerate.Next(5, 10000);

                                                 for (int i = 0; i < randomNumber; i++)
                                                 {
                                                     var performanceEvent = new PerformanceEvent();

                                                     performanceEvent.Category = eventType;

                                                     var randomValue = new Random();
                                                     var valueToUse = randomValue.Next(1, 30);

                                                     //Calculate method execution duration
                                                     var duration = valueToUse;

                                                     //Calculate time referenced to Unix epoch
                                                     //var startOfEpoch = new DateTime(1970, 1, 1);
                                                     //long ticksSinceEpoch = DateTime.Now.Ticks - startOfEpoch.Ticks;

                                                     performanceEvent.Value = duration;
                                                     performanceEvent.TimeStamp = DateTime.Now.Ticks;
                                                     performanceEvent.AddContextValue("Source",
                                                                                      "PerformanceEventGenerator");
                                                     performanceEvent.AddContextValue("TITLE", "Mr");
                                                     performanceEvent.AddContextValue("NAME",
                                                                                      "Name_" +
                                                                                      Guid.NewGuid().ToString());
                                                     performanceEvent.AddContextValue("SURNAME",
                                                                                      "Surname_" +
                                                                                      Guid.NewGuid().ToString());
                                                     performanceEvent.AddContextValue("EMAIL",
                                                                                      "SomeEmailAddress@testmail.coco");

                                                     Console.WriteLine("{0} - Writing new event for {1}", DateTime.Now,eventType);
                                                     service.SendEvent(performanceEvent);
                                                 }
                                             });
        }
    }
}